package com.canbot.u05.activity;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.canbot.u05.R;

/**
 * 文字转语音
 *
 * @author ruandan
 */
public class Txt2VoiceActivity extends BaseActivityNoTitle implements OnClickListener {

        // private LinearLayout llTxt;
        // private LinearLayout llRecord;
        // private EditText etTxt;
        private Button btnDelete;

        private Button btnYes;

        private RelativeLayout mRelativeLayout;

        private Button mBack;

        private TextView mTextView;

        private Button mBtnPlay;

        private EditText mEtRelayVoice;

//        private Sound mSound;

        private AnimationDrawable anim_Load;

        private TextView tvTime;

        private boolean counting = true;

        private int sumSencond = 0;// 总秒数

        private boolean isCancle = false;// 录音是否取消

        private  String voiceStr;

        // 计时器
        Handler mHandler = new Handler() {
                int s1 = 0;

                int s2 = 0;

                int m1 = 0;

                int m2 = 0;

                public void handleMessage(android.os.Message msg) {
                        sumSencond++;
                        s1 = (sumSencond % 60) / 10;
                        s2 = (sumSencond % 60) % 10;
                        m1 = (sumSencond / 60) % 60 / 10;
                        m2 = (sumSencond / 60) % 60 % 10;
                        tvTime.setText(m1 + m2 + ":" + s1 + s2);
                }

                ;
        };

        private TxtReceiver mReceiver;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_txt2_voice);
                init();
                initView();
        }

        private void init() {
//                mSound = new Sound(this);
        }

        @Override
        protected void onResume() {
                super.onResume();
                mReceiver = new TxtReceiver();// 注册广播
                IntentFilter filter = new IntentFilter();
                filter.addAction("ACTION_VOICE_TO_TXT");
                registerReceiver(mReceiver, filter);
        }

        @Override
        protected void onDestroy() {
                super.onDestroy();
                unregisterReceiver(mReceiver);// 注销广播
//                mSound.cancle();
        }

        private void initView() {
                mRelativeLayout = (RelativeLayout) findViewById(R.id.head_language);
                mBack = (Button) mRelativeLayout.findViewById(R.id.btn_head_back);


                mBtnPlay = (Button) findViewById(R.id.btn_voice);
                mEtRelayVoice = (EditText) findViewById(R.id.et_txt);
                mTextView = (TextView) mRelativeLayout.findViewById(R.id.tv_head);
                mTextView.setText("文字转语音");

                btnDelete = (Button) findViewById(R.id.btnDelete);
                btnYes = (Button) findViewById(R.id.btnYes);
                btnDelete.setOnClickListener(this);
                btnYes.setOnClickListener(this);
                mBack.setOnClickListener(this);
                mBtnPlay.setOnClickListener(this);
                // 最多输入30个字符
                mEtRelayVoice.setFilters(
                        new InputFilter[]{new InputFilter.LengthFilter(200)});
                mEtRelayVoice.addTextChangedListener(new TextWatcher() {

                        @Override
                        public void onTextChanged(CharSequence s, int start, int before,
                                                  int count) {
                                if (count > 0) {
                                        btnYes.setBackgroundResource(
                                                R.drawable.icon_dialogyes_00);
                                }
                                else {
                                        btnYes.setBackgroundResource(
                                                R.drawable.icon_dialogyes_01);
                                }

                        }

                        @Override
                        public void beforeTextChanged(CharSequence s, int start, int count,
                                                      int after) {
                                // TODO Auto-generated method stub

                        }

                        @Override
                        public void afterTextChanged(Editable s) {
                        }
                });

        }

        @Override
        public void onClick(View v) {
                switch (v.getId()) {
                        case R.id.btn_head_back:
                                clearAllData();// 清除所有数据
                                break;
                        case R.id.btnDelete:
                                clearAllData();// 清除所有数据
                                break;
                        case R.id.btnYes:
                                if (!mEtRelayVoice.getText().toString().isEmpty()) {
                                        voiceStr = mEtRelayVoice
                                                .getText().toString();
                                        Intent dataBack = new Intent();
                                        dataBack.putExtra("voiceStr", voiceStr);
                                        this.setResult(Activity.RESULT_OK, dataBack);

                                        finish();
                                }
                                break;

                        case R.id.btn_voice:// tts
                                String txt = mEtRelayVoice.getText().toString();
                                if (null != txt && !txt.equals("")) {
//                                        mSound.playTTs(txt);

                                }
                                else {
                                        txt = "您还没有输入文字";
//                                        mSound.playTTs(txt);
                                }
                                break;
                }

        }

        /**
         * 清除本界面生产的所有数据
         */
        private void clearAllData() {
//                RecordActivity.voiceType = 0;// 未制作语音
                voiceStr = "";// 清空文本
                this.setResult(Activity.RESULT_CANCELED);
                finish();
        }


        /**
         * 录音转文本结果
         *
         * @author ruan123
         */
        class TxtReceiver extends BroadcastReceiver {

                @Override
                public void onReceive(Context context, Intent intent) {
                        if (intent.getAction().equals("ACTION_VOICE_TO_TXT") && !isCancle) {
                                String record2txt = intent.getStringExtra("txt");
                                mEtRelayVoice.setText(record2txt);
                        }

                }

        }
}
